﻿using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 完工订单定时任务.models;

namespace 完工订单定时任务.报表
{
    public partial class FormPLC错误单据计算 : Form
    {
        public FormPLC错误单据计算()
        {
            InitializeComponent();
        }

        private void button查询_Click(object sender, EventArgs e)
        {
            DataTable dt错误 = SqlSugarHelp.scope.Ado.GetDataTable($"select distinct vbillcode from 材料数量中间变小记录 where 材料数量中间变小记录.vbillcode = '{textBox单号.Text.Trim()}'");
            if (dt错误 == null || dt错误.Rows.Count <= 0)
            {
                MessageBox.Show($"{textBox单号.Text.Trim()}不在PLC数据错误记录里");
                return;
            }

            Form1 form = new Form1();
            需计算工单DTO 需计算工单dto = form.按钮计算(textBox产线.Text.Trim(),textBox单号.Text.Trim());
            if (需计算工单dto != null)
            {
                //计算时先删除
                string sqldelete = $@"
                        DELETE FROM ncmaterialoutdetail WHERE vbillcode = '{需计算工单dto.vbillcode}';
                        ";
                int r = SqlSugarHelp.scope.Ado.ExecuteCommand(sqldelete);
                string tablename = "";
                if (textBox产线.Text.Trim() == "1")
                {
                    tablename = "scll";
                }
                else if (textBox产线.Text.Trim() == "2")
                {
                    tablename = "scll二线";
                }
                else
                {
                    MessageBox.Show($"产线应该是1或者2");
                    return;
                }

                form.计算辅挤明细(需计算工单dto.vbillcode, tablename);
                form.计算ABR明细(需计算工单dto.vbillcode, tablename);
                form.计算主挤明细(需计算工单dto.vbillcode, tablename);
                string sql = $@" select REPLACE(REPLACE(vbillcode, CHAR(10), ''), CHAR(13), '') vbillcode, REPLACE(REPLACE(lineno, CHAR(10), ''), CHAR(13), '') lineno,current_materialName 物料名称,sum(quantity_diff) 用量
                                    from ncmaterialoutdetail  
                                    where quantity_diff > 0 and vbillcode = '{需计算工单dto.vbillcode}'
                                    GROUP BY REPLACE(REPLACE(vbillcode, CHAR(10), ''), CHAR(13), '') , REPLACE(REPLACE(lineno, CHAR(10), ''), CHAR(13), '') ,current_materialName";
                DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
                dataGridView1.DataSource = dt;
                dataGridView1.Refresh();
                SqlSugarHelp.scope.Ado.ExecuteCommand(sqldelete);
                form.Dispose();

                string sql2 = $@"SELECT  sum(quantity_diff) 投入原料合计 FROM `plc`.`ncmaterialoutdetail` where vbillcode = '{需计算工单dto.vbillcode}' and quantity_diff > 0";
                decimal? 投入原料合计2 = SqlSugarHelp.scope.Ado.GetDecimal(sql2);
                //更新到拉伸成品率的 投入原料实时合计数量
                string sql3 = $@"UPDATE VIEW_拉伸报表 SET 投入原料实时合计数量 = {投入原料合计2} WHERE 工单号 = '{需计算工单dto.vbillcode}'";
                int r3 =SqlSugarHelp.scope.Ado.ExecuteCommand(sql3);
            }
            else
            {
                MessageBox.Show($"未查询到{textBox产线.Text.Trim()}线{textBox单号.Text.Trim()}工单信息");
            }
            
        }

        private void button导出_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource != null)
            {
                DataTable dt = (DataTable)dataGridView1.DataSource;
                string fileName = $"{textBox单号.Text.Trim()}材料计算结果" + DateTime.Now.ToFileTime() + ".xlsx";
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "All files (*.*)|*.*";
                saveFileDialog.FileName = fileName;
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    MiniExcel.SaveAs(saveFileDialog.FileName, dt);
                    MessageBox.Show("导出成功");
                }
            }
            else
            {
                MessageBox.Show($"请先查询数据");
            }
        }
    }
}
